using System;
using System.Collections.Generic;
using System.Text;
using Wrath.Xml;
using System.IO;
using System.Windows.Forms;

namespace Wrath.RuleForge
{
    public static class FileBroker
    {
        private static Dictionary<string, FileParser> fileParsers;
        private static List<Exception> fileParseExceptions;

        public static Dictionary<string, FileParser> Files { get { return fileParsers; } }
 
        public static List<Exception> LoadingExceptions { get { return fileParseExceptions; } }

        static FileBroker()
        {
            fileParsers = new Dictionary<string, FileParser>();
            fileParseExceptions = new List<Exception>();            
        }

        #region Debugging Files

        public class DebugFile : IDisposable
        {
            private TextWriter debugFile;

            public DebugFile(string filename)
            {
                debugFile = new StreamWriter(filename, true);
                debugFile.WriteLine();
                debugFile.WriteLine("-- Session Started: " + DateTime.Now.ToString() + " --------------------");
                debugFile.WriteLine();
                debugFile.Flush();

                EventBroker.DebugMessage += new EventBroker.MessageEventHandler(EventBroker_DebugMessage);
            }

            private void EventBroker_DebugMessage(EventBroker.MessageEventArgs messageEventArgs)
            {
                debugFile.WriteLine(messageEventArgs.Message);
                debugFile.Flush();
            }

            public void WriteLine() { debugFile.WriteLine(); }
            public void WriteLine(string line) { debugFile.WriteLine(line); }
            public void WriteLine(string format, params object[] args) { debugFile.WriteLine(format, args); }

            #region IDisposable Members

            public void Dispose()
            {
                try
                {
                    debugFile.WriteLine();
                    debugFile.WriteLine("-- Session Ended: " + DateTime.Now.ToString() + " --------------------");
                    debugFile.Flush();
                }
                catch 
                {                    
                    
                }

                debugFile.Close();
            }

            #endregion
        }

        public static DebugFile Debug;

        #endregion
    }
}
